﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;


namespace FYPHP
{
    public partial class TransactionHistory : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdTransactionHistory;
                SqlDataReader dtrTransactionHistory;
                conn.Open();

                cmdTransactionHistory = new SqlCommand("SELECT * from Transactions t, Users u WHERE t.t_seller=u.u_id AND t.u_id=@UID  AND (t_status=@TheFirstStatus OR t_status=@TheSecondStatus OR t_status=@TheThirdStatus OR t_status=@TheFourthStatus)", conn);
                cmdTransactionHistory.Parameters.AddWithValue("@UID", Session["ID"].ToString());
                cmdTransactionHistory.Parameters.AddWithValue("@TheFirstStatus","CartCompleted");
                cmdTransactionHistory.Parameters.AddWithValue("@TheSecondStatus", "BuyNowCompleted");
                cmdTransactionHistory.Parameters.AddWithValue("@TheThirdStatus", "CartShipped");
                cmdTransactionHistory.Parameters.AddWithValue("@TheFourthStatus", "BuyNowShipped");

                dtrTransactionHistory = cmdTransactionHistory.ExecuteReader();


                ItemList.DataSource = dtrTransactionHistory;
                ItemList.DataBind();

                dtrTransactionHistory.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
                MessageBox(ex.Message);
            }
        }

     
        protected String formatRef(object tid)
        {
            String reference = "REF-" + String.Format("{0:00000}", int.Parse(tid.ToString()));
            return reference;
        }

        protected void ItemList_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {
           
            if (e.CommandName == "View")
            { 
                int tid = Convert.ToInt32(e.CommandArgument.ToString());
                Redirect("ReferencePrinting.aspx?id=" + tid);

            }
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.open('" + msg + "', '_blank');</script>";
            Page.Controls.Add(lbl);
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

    }
}